package com.simtop.car.service.imp;

import com.github.pagehelper.PageHelper;
import com.simtop.car.common.ServerResponse;
import com.simtop.car.mapper.CarMapper;
import com.simtop.car.pojo.BrandType;
import com.simtop.car.pojo.User;
import com.simtop.car.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    public CarMapper carMapper;

    @Override
    public ServerResponse<List<BrandType>> getCarBrandType() {
        PageHelper.clearPage();
        List<BrandType> brandTypeList= carMapper.getCarBrandType();
        try {
            brandTypeList.forEach(brandType -> {
                String firmIntroduction=carMapper.getFirmIntroduction(brandType.getBrandName());
                brandType.setFirmIntroduction(firmIntroduction);
            });
        } catch (Exception e) {
            e.printStackTrace();
            ServerResponse.createByErrorMsg("数据有误");
        }
        return ServerResponse.createBySuccess(brandTypeList);
    }

    @Override
    public ServerResponse<List<BrandType>> addCarBrandType(BrandType brandType, User u) {
        BrandType brandType1=carMapper.checkBrandType(brandType.getBrandName(),u.getId());
        if(!ObjectUtils.isEmpty(brandType1)){
            return ServerResponse.createByErrorMsg("该汽车品牌已存在");
        }
        List<BrandType> brandTypeList=new ArrayList<>();
        brandType.setUserId(u.getId());
        carMapper.addCarBrandType(brandType);
        brandTypeList.add(brandType);
        return ServerResponse.createBySuccess(brandTypeList);
    }
}
